Include a header file required for build on mac 10.4
[supercollider.git] / Help / Other Topics / Randomness.html
blobe1e7c68f200fe862345dc441d96aeb067fb3d668
1 <html><head>
2 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
3 <meta http-equiv="Content-Style-Type" content="text/css">
4 <title></title>
5 <meta name="Generator" content="Cocoa HTML Writer">
6 <meta name="CocoaVersion" content="824.41">
7 <style type="text/css">
8 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
9 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
10 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
11 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
12 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #882917}
13 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
14 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
15 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #ba1100}
16 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
17 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0027ee}
18 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ff}
19 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0027ee}
20 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #001fbf}
21 span.s1 {color: #000000}
22 span.s2 {color: #882917}
23 span.s3 {color: #0036ad}
24 span.s4 {color: #0042f1}
25 span.s5 {color: #001fbf}
26 span.s6 {color: #606060}
27 span.s7 {color: #4f6926}
28 span.s8 {font: 12.0px Helvetica}
29 span.s9 {color: #ba1100}
30 span.s10 {text-decoration: underline}
31 span.s11 {text-decoration: underline ; color: #0000ff}
32 span.s12 {text-decoration: underline ; color: #0027ee}
33 span.s13 {color: #002aff}
34 span.s14 {font: 9.0px Monaco; text-decoration: underline}
35 span.s15 {color: #1d7008}
36 span.Apple-tab-span {white-space:pre}
37 </style>
38 </head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
39 <p class="p1"><span class="Apple-style-span" style="font-size: 18px; font-weight: bold; ">Randomness in SC</span><br></p>
40 <p class="p1"><br></p>
41 <p class="p3">As in any computer program, there are no "truly random" number generators in SC.<span class="Apple-converted-space"> </span></p>
42 <p class="p3">They are pseudo-random, meaning they use very complex, but deterministic<span class="Apple-converted-space"> </span></p>
43 <p class="p3">algorithms to generate sequences of numbers that are long enough and complicated enough<span class="Apple-converted-space"> </span></p>
44 <p class="p3">to seem "random" for human beings. (i.e. the patterns are too complex for us to detect.)</p>
45 <p class="p1"><br></p>
46 <p class="p3">If you start a random number generator algorithm with the same "seed" number<span class="Apple-converted-space"> </span></p>
47 <p class="p3">several times, you get the same sequence of random numbers.<span class="Apple-converted-space"> </span></p>
48 <p class="p3">(See example below, <i>randomSeed</i>)</p>
49 <p class="p1"><br></p>
50 <p class="p1"><br></p>
51 <p class="p4"><b>Create single random numbers:</b></p>
52 <p class="p1"><br></p>
53 <p class="p1"><br></p>
54 <p class="p3"><b>1. Between zero and &lt;number&gt;:</b></p>
55 <p class="p1"><br></p>
56 <p class="p5"><span class="s1">5.rand<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// evenly distributed.</p>
57 <p class="p6"><br></p>
58 <p class="p5"><span class="s1">1.0.linrand<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// probability decreases linearly from 0 to &lt;number&gt;.</p>
59 <p class="p1"><br></p>
60 <p class="p1"><br></p>
61 <p class="p3"><b>2. Between -&lt;number&gt; and &lt;number&gt;:</b></p>
62 <p class="p1"><br></p>
63 <p class="p5"><span class="s1">5.0.rand2<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// evenly distributed.</p>
64 <p class="p6"><br></p>
65 <p class="p5"><span class="s1">10.bilinrand<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// probability is highest around 0,<span class="Apple-converted-space"> </span></p>
66 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// decreases linearly toward +-&lt;number&gt;.</p>
67 <p class="p6"><br></p>
68 <p class="p5"><span class="s1">1.0.sum3rand<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// quasi-gaussian, bell-shaped distribution.</p>
69 <p class="p1"><br></p>
70 <p class="p3"><b>3. Within a given range:</b></p>
71 <p class="p1"><br></p>
72 <p class="p5"><span class="s1">rrand(24, 48)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// linear distribution in the given range.</p>
73 <p class="p6"><br></p>
74 <p class="p5"><span class="s1">exprand(0.01, 1)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// exponential distribution;<span class="Apple-converted-space"> </span></p>
75 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// both numbers must have the same sign.</p>
76 <p class="p1"><br></p>
77 <p class="p1"><br></p>
78 <p class="p3"><b>Test them multiple times with a do loop:</b></p>
79 <p class="p1"><br></p>
80 <p class="p1"><br></p>
81 <p class="p7">20.do({ 5.rand.postln; });<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">// evenly distributed</span></p>
82 <p class="p6"><br></p>
83 <p class="p5"><span class="s1">20.do({ 1.0.linrand.postln; });<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// probability decreases linearly from 0 to 1.0</p>
84 <p class="p6"><br></p>
85 <p class="p6"><br></p>
86 <p class="p7">20.do({ 5.0.rand2.postln; });<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">// even</span></p>
87 <p class="p6"><br></p>
88 <p class="p5"><span class="s1">20.do({ 10.bilinrand.postln; });<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// probability is highest around 0,<span class="Apple-converted-space"> </span></p>
89 <p class="p5"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// decreases linearly toward +-&lt;number&gt;.</p>
90 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
91 <p class="p7">20.do({ 1.0.sum3rand.postln; });<span class="Apple-tab-span"> </span><span class="s2">// quasi-gaussian, bell-shaped.</span></p>
92 <p class="p1"><br></p>
93 <p class="p1"><br></p>
94 <p class="p1"><br></p>
95 <p class="p3"><b>Collect the results in an array:</b></p>
96 <p class="p1"><br></p>
97 <p class="p7"><span class="s3">Array</span>.fill(10, { 1000.linrand }).postln;<span class="Apple-converted-space"> </span></p>
98 <p class="p6"><br></p>
99 <p class="p5">// or more compact:<span class="Apple-converted-space"> </span></p>
100 <p class="p6"><br></p>
101 <p class="p7">{ 1.0.sum3rand }.dup(100)<span class="Apple-converted-space"> </span></p>
102 <p class="p6"><br></p>
103 <p class="p5">// or:</p>
104 <p class="p6"><br></p>
105 <p class="p7">({ 1.0.sum3rand } ! 100)</p>
106 <p class="p1"><br></p>
107 <p class="p1"><br></p>
108 <p class="p1"><br></p>
109 <p class="p3"><b>You can seed a random generator in order to repeat<span class="Apple-converted-space"> </span></b></p>
110 <p class="p3"><b>the same sequence of random numbers:</b></p>
111 <p class="p1"><br></p>
112 <p class="p7">(</p>
113 <p class="p7">5.do({<span class="Apple-converted-space"> </span></p>
114 <p class="p7"><span class="Apple-tab-span"> </span><span class="s3">thisThread</span>.randSeed = 4;</p>
115 <p class="p7"><span class="Apple-tab-span"> </span><span class="s3">Array</span>.fill(10, { 1000.linrand}).postln;<span class="Apple-converted-space"> </span></p>
116 <p class="p7">});</p>
117 <p class="p7">)</p>
118 <p class="p6"><br></p>
119 <p class="p5">// Just to check, no seeding:</p>
120 <p class="p6"><br></p>
121 <p class="p7">(</p>
122 <p class="p7">5.do({ <span class="s3">Array</span>.fill(10, { 1000.linrand }).postln; });</p>
123 <p class="p7">)</p>
124 <p class="p6"><br></p>
125 <p class="p5">// see also <a href="../Core/Kernel/randomSeed.html"><span class="s4">randomSeed</span></a>.</p>
126 <p class="p6"><br></p>
127 <p class="p1"><br></p>
128 <p class="p1"><br></p>
129 <p class="p3"><b>Demonstrate the various statistical distributions visually, with histograms:</b></p>
130 <p class="p1"><br></p>
131 <p class="p3">[plot may not work in non-Mac SC3 versions.]</p>
132 <p class="p1"><br></p>
133 <p class="p7"><span class="s5">Array</span>.fill(500, {<span class="Apple-converted-space">  </span>1.0.rand }).plot(<span class="s6">"Sequence of 500x 1.0.rand"</span>);</p>
134 <p class="p6"><br></p>
135 <p class="p7"><span class="s5">Array</span>.fill(500, {<span class="Apple-converted-space">  </span>1.0.linrand }).plot(<span class="s6">"Sequence of 500x 1.0.linrand"</span>);</p>
136 <p class="p6"><br></p>
137 <p class="p7"><span class="s5">Array</span>.fill(500, {<span class="Apple-converted-space">  </span>1.0.sum3rand }).plot(<span class="s6">"Sequence of 500x 1.0.sum3rand"</span>);</p>
138 <p class="p6"><br></p>
139 <p class="p8">// Use a histogram to display how often each (integer)<span class="Apple-converted-space"> </span></p>
140 <p class="p8">// occurs in a collection of random numbers, :</p>
141 <p class="p7">(</p>
142 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, maxValue = 500, numVals = 10000, numBins = 500;</p>
143 <p class="p6"><br></p>
144 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { maxValue.rand; });</p>
145 <p class="p7">histogram = randomNumbers.histo(numBins, 0, maxValue);</p>
146 <p class="p7">histogram.plot(<span class="s6">"histogram for rand 0 - "</span> ++ maxValue);</p>
147 <p class="p7">)</p>
148 <p class="p6"><br></p>
149 <p class="p1"><br></p>
150 <p class="p1"><br></p>
151 <p class="p3"><b>A histogram for linrand:</b></p>
152 <p class="p1"><br></p>
153 <p class="p7">(</p>
154 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, maxValue = 500.0, numVals = 10000, numBins = 500;</p>
155 <p class="p6"><br></p>
156 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { maxValue.linrand; });</p>
157 <p class="p7">histogram = randomNumbers.histo(numBins, 0, maxValue);</p>
158 <p class="p7">histogram.plot(<span class="s6">"histogram for linrand 0 - "</span> ++ maxValue);</p>
159 <p class="p7">)</p>
160 <p class="p1"><br></p>
161 <p class="p3"><b>A histogram for bilinrand:</b></p>
162 <p class="p1"><br></p>
163 <p class="p7">(</p>
164 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;</p>
165 <p class="p6"><br></p>
166 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { maxValue.bilinrand; });</p>
167 <p class="p7">histogram = randomNumbers.histo(numBins, minValue, maxValue);</p>
168 <p class="p7">histogram.plot(<span class="s6">"histogram for bilinrand"</span> + minValue + <span class="s6">"to"</span> + maxValue);</p>
169 <p class="p7">)</p>
170 <p class="p1"><br></p>
171 <p class="p1"><br></p>
172 <p class="p1"><br></p>
173 <p class="p3"><b>A histogram for exprand:</b></p>
174 <p class="p1"><br></p>
175 <p class="p7">(</p>
176 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, minValue = 5.0, maxValue = 500, numVals = 10000, numBins = 500;</p>
177 <p class="p6"><br></p>
178 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { exprand(minValue, maxValue); });</p>
179 <p class="p7">histogram = randomNumbers.histo(numBins, minValue, maxValue);</p>
180 <p class="p7">histogram.plot(<span class="s6">"histogram for exprand: "</span> ++ minValue ++ <span class="s6">" to "</span> ++ maxValue);</p>
181 <p class="p7">)</p>
182 <p class="p1"><br></p>
183 <p class="p1"><br></p>
184 <p class="p3"><b>And for sum3rand (cheap quasi-gaussian):</b></p>
185 <p class="p1"><br></p>
186 <p class="p1"><br></p>
187 <p class="p7">(</p>
188 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;</p>
189 <p class="p6"><br></p>
190 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { maxValue.sum3rand; });</p>
191 <p class="p7">histogram = randomNumbers.histo(numBins, minValue, maxValue);</p>
192 <p class="p7">histogram.plot(<span class="s6">"histogram for sum3rand "</span> ++ minValue ++ <span class="s6">" to "</span> ++ maxValue);</p>
193 <p class="p7">)</p>
194 <p class="p1"><br></p>
195 <p class="p1"><br></p>
196 <p class="p1"><br></p>
197 <p class="p3">All of the single-number methods also work for (Sequenceable)Collections,<span class="Apple-converted-space"> </span></p>
198 <p class="p3">simply by applying the given random message to each element of the collection:</p>
199 <p class="p1"><br></p>
200 <p class="p5"><span class="s1">[ 1.0, 10, 100.0, </span><span class="s7">\aSymbol</span><span class="s1"> ].rand.postln;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// note: Symbols are left as they are.</p>
201 <p class="p7"><span class="s3">List</span>[ 10, -3.0, <span class="s7">\aSymbol</span> ].sum3rand.postln;</p>
202 <p class="p6"><br></p>
203 <p class="p1"><br></p>
204 <p class="p1"><br></p>
205 <p class="p1"><br></p>
206 <p class="p3"><b>Arbitrary random distributions</b></p>
207 <p class="p1"><br></p>
208 <p class="p3">An <i>integral table</i> can be used to create an arbitrary random distribution quite efficiently. The table</p>
209 <p class="p3">building is expensive though. The more points there are in the random table, the more accurate the</p>
210 <p class="p3">distribution.</p>
211 <p class="p1"><br></p>
212 <p class="p7">(</p>
213 <p class="p7"><span class="s5">var</span> randomNumbers, histogram, distribution, randomTable, randTableSize=200;</p>
214 <p class="p7"><span class="s5">var</span> minValue = -250, maxValue = 250, numVals = 10000, numBins = 500;</p>
215 <p class="p6"><br></p>
216 <p class="p8">// create some random distribution with values between 0 and 1</p>
217 <p class="p7">distribution = <span class="s5">Array</span>.fill(randTableSize,<span class="Apple-converted-space"> </span></p>
218 <p class="p7"><span class="Apple-tab-span"> </span>{ <span class="s5">arg</span> i; (i/ randTableSize * 35).sin.max(0) * (i / randTableSize) }</p>
219 <p class="p7">);</p>
220 <p class="p6"><br></p>
221 <p class="p8">// render a randomTable</p>
222 <p class="p7">randomTable = distribution.asRandomTable;<span class="Apple-converted-space"> </span></p>
223 <p class="p6"><br></p>
224 <p class="p8">// get random numbers, scale them</p>
225 <p class="p6"><br></p>
226 <p class="p7">randomNumbers = <span class="s5">Array</span>.fill(numVals, { randomTable.tableRand * (maxValue - minValue) + minValue; });<span class="Apple-converted-space"> </span></p>
227 <p class="p7">histogram = randomNumbers.histo(numBins, minValue, maxValue);</p>
228 <p class="p6"><br></p>
229 <p class="p6"><br></p>
230 <p class="p9"><span class="s1">histogram.plot(</span>"this is the histogram we got"<span class="s1">);</span></p>
231 <p class="p9"><span class="s1">distribution.plot(</span>"this was the histogram we wanted"<span class="s1">);</span></p>
232 <p class="p7">)</p>
233 <p class="p1"><br></p>
234 <p class="p1"><br></p>
235 <p class="p1"><br></p>
236 <p class="p1"><br></p>
237 <p class="p1"><br></p>
238 <p class="p1"><br></p>
239 <p class="p4"><b>Random decisions:</b></p>
240 <p class="p1"><br></p>
241 <p class="p1"><br></p>
242 <p class="p3"><b>coin</b> <span class="Apple-tab-span"> </span>simulates a coin toss and results in true or false.<span class="Apple-converted-space"> </span></p>
243 <p class="p3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>1.0 is always true, 0.0 is always false, 0.5 is 50:50 chance.</p>
244 <p class="p1"><br></p>
245 <p class="p7">20.do({ 0.5.coin.postln });</p>
246 <p class="p1"><br></p>
247 <p class="p3">biased random decision can be simulated bygenerating a single value<span class="Apple-converted-space"> </span></p>
248 <p class="p3">and check against a threshhold:</p>
249 <p class="p1"><br></p>
250 <p class="p7">20.do({ (1.0.linrand &gt; 0.5).postln });</p>
251 <p class="p7">20.do({ (exprand(0.05, 1.0) &gt; 0.5).postln });</p>
252 <p class="p1"><br></p>
253 <p class="p4"><b>Generating Collections of random numbers:</b></p>
254 <p class="p1"><br></p>
255 <p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// size, minVal, maxVal<span class="Apple-converted-space"> </span></p>
256 <p class="p7"><span class="s5">Array</span>.rand(7, 0.0, 1.0).postln;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
257 <p class="p6"><br></p>
258 <p class="p8">// is short for:</p>
259 <p class="p6"><br></p>
260 <p class="p7"><span class="s5">Array</span>.fill(7, { rrand(0.0, 1.0) }).postln;<span class="Apple-tab-span"> </span></p>
261 <p class="p6"><br></p>
262 <p class="p6"><br></p>
263 <p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// size, minVal, maxVal<span class="Apple-converted-space"> </span></p>
264 <p class="p7"><span class="s5">List</span>.linrand(7, 10.0, 15.0).postln;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
265 <p class="p6"><br></p>
266 <p class="p8">// is short for:</p>
267 <p class="p6"><br></p>
268 <p class="p7"><span class="s5">List</span>.fill(7, { 10 + 5.0.linrand }).postln;<span class="Apple-tab-span"> </span></p>
269 <p class="p6"><br></p>
270 <p class="p7"><span class="s5">Signal</span>.exprand(10, 0.1, 1);</p>
271 <p class="p6"><br></p>
272 <p class="p7"><span class="s5">Signal</span>.rand2(10, 1.0);</p>
273 <p class="p6"><br></p>
274 <p class="p1"><br></p>
275 <p class="p1"><br></p>
276 <p class="p4"><b>Random choice from Collections</b></p>
277 <p class="p1"><span class="Apple-tab-span"> </span></p>
278 <p class="p3"><b>choose </b><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>equal chance for each element.</p>
279 <p class="p1"><br></p>
280 <p class="p7">10.do({ [ 1, 2, 3 ].choose.postln });<span class="s8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span></p>
281 <p class="p1"><br></p>
282 <p class="p1"><br></p>
283 <p class="p3"><b>Weighted choice:</b></p>
284 <p class="p1"><br></p>
285 <p class="p3"><b>wchoose(weights)</b><span class="Apple-tab-span"> </span>An array of weights sets the chance for each element.</p>
286 <p class="p1"><br></p>
287 <p class="p7">10.do({ [ 1, 2, 3 ].wchoose([0.1, 0.2, 0.7]).postln });<span class="Apple-tab-span"> </span></p>
288 <p class="p1"><br></p>
289 <p class="p1"><br></p>
290 <p class="p1"><br></p>
291 <p class="p4"><b>Randomize the order of a Collection:<span class="Apple-converted-space"> </span></b><span class="s8"><span class="Apple-tab-span"> </span></span></p>
292 <p class="p1"><br></p>
293 <p class="p3"><b>scramble</b></p>
294 <p class="p1"><br></p>
295 <p class="p7"><span class="s5">List</span>[ 1, 2, 3, 4, 5 ].scramble.postln;</p>
296 <p class="p1"><br></p>
297 <p class="p1"><br></p>
298 <p class="p4"><b>Generate random numbers without duplicates:<span class="Apple-converted-space"> </span></b><span class="s8"><span class="Apple-tab-span"> </span></span></p>
299 <p class="p1"><br></p>
300 <p class="p7">f = { <span class="s5">|n=8, min=0, max=7|</span> (min..max).scramble.keep(n) };</p>
301 <p class="p7">f.value(8, 0, 7)</p>
302 <p class="p6"><br></p>
303 <p class="p1"><br></p>
304 <p class="p1"><br></p>
305 <p class="p4"><b>Randomly group a Collection:<span class="Apple-tab-span"> </span></b></p>
306 <p class="p1"><br></p>
307 <p class="p3"><b>curdle(probability)</b></p>
308 <p class="p1"><br></p>
309 <p class="p3">The probability argument sets the chance that two adjacent elements will be separated.</p>
310 <p class="p1"><br></p>
311 <p class="p7">[ 1, 2, 3, 4, 5, 6, 7, 8 ].curdle(0.2).postln;<span class="Apple-tab-span"> </span><span class="s9">// big groups</span></p>
312 <p class="p6"><br></p>
313 <p class="p7">[ 1, 2, 3, 4, 5, 6, 7, 8 ].curdle(0.75).postln;<span class="Apple-tab-span"> </span><span class="s9">// small groups</span></p>
314 <p class="p1"><br></p>
315 <p class="p1"><br></p>
316 <p class="p1"><br></p>
317 <p class="p1"><br></p>
318 <p class="p1"><br></p>
319 <p class="p1"><br></p>
320 <p class="p4"><b>Random signal generators, i.e. UGens:</b></p>
321 <p class="p1"><br></p>
322 <p class="p10"><span class="s10"><a href="../UGens/Noise/PinkNoise.html">PinkNoise</a></span></p>
323 <p class="p10"><span class="s10"><a href="../UGens/Noise/WhiteNoise.html">WhiteNoise</a></span></p>
324 <p class="p10"><span class="s10"><a href="../UGens/Noise/GrayNoise.html">GrayNoise</a></span></p>
325 <p class="p10"><span class="s10"><a href="../UGens/Noise/BrownNoise.html">BrownNoise</a></span></p>
326 <p class="p10"><span class="s10"><a href="../UGens/Noise/PinkerNoise.html">PinkerNoise</a></span></p>
327 <p class="p10"><span class="s10"><a href="../UGens/Noise/ClipNoise.html">ClipNoise</a></span></p>
328 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFNoise0.html">LFNoise0</a></span></p>
329 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFNoise1.html">LFNoise1</a></span></p>
330 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFNoise2.html">LFNoise2</a></span></p>
331 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFClipNoise.html">LFClipNoise</a></span></p>
332 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFDNoise0.html">LFDNoise0</a></span></p>
333 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFDNoise1.html">LFDNoise1</a></span></p>
334 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFDNoise3.html">LFDNoise3</a></span></p>
335 <p class="p10"><span class="s10"><a href="../UGens/Noise/LFDClipNoise.html">LFDClipNoise</a></span></p>
336 <p class="p10"><span class="s10"><a href="../UGens/Noise/Dust.html">Dust</a></span></p>
337 <p class="p10"><span class="s10"><a href="../UGens/Noise/Dust2.html">Dust2</a></span></p>
338 <p class="p10"><span class="s10"><a href="../UGens/Noise/Crackle.html">Crackle</a></span></p>
339 <p class="p11"><span class="s10"><a href="../UGens/Triggers/TWChoose.html">TWChoose<span class="s11"></span></a></span></p>
340 <p class="p1"><br></p>
341 <p class="p1"><br></p>
342 <p class="p1"><br></p>
343 <p class="p1"><br></p>
344 <p class="p3"><b>UGens that generate random numbers once, or on trigger:</b></p>
345 <p class="p1"><br></p>
346 <p class="p3"><span class="s12"><a href="../UGens/Random/Rand.html">Rand</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>uniform distribution of float between (lo, hi), as for numbers.</p>
347 <p class="p3"><span class="s12"><a href="../UGens/Random/IRand.html">IRand</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>uniform distribution of integer numbers.</p>
348 <p class="p3"><span class="s12"><a href="../UGens/Random/TRand.html">TRand</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>uniform distribution of float numbers, triggered</p>
349 <p class="p3"><span class="s12"><a href="../UGens/Random/TIRand.html">TIRand</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>uniform distribution of integer numbers, triggered</p>
350 <p class="p3"><span class="s12"><a href="../UGens/Random/LinRand.html">LinRand</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>skewed distribution of float numbers, triggered</p>
351 <p class="p3"><span class="s12"><a href="../UGens/Random/NRand.html">NRand</a></span> <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>sum of n uniform distributions, approximates gaussian distr. with higher n.</p>
352 <p class="p3"><span class="s12"><a href="../UGens/Random/ExpRand.html">ExpRand</a></span> <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>exponential distribution</p>
353 <p class="p3"><span class="s12"><a href="../UGens/Random/TExpRand.html">TExpRand</a></span><span class="Apple-tab-span"> </span>exponential distribution, triggered</p>
354 <p class="p3"><span class="s12"><a href="../UGens/Random/CoinGate.html">CoinGate</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>statistical gate for a trigger</p>
355 <p class="p3"><span class="s12"><a href="../UGens/Triggers/TWindex.html">TWindex</a></span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>triggered weighted choice between a list</p>
356 <p class="p1"><br></p>
357 <p class="p3"><span class="Apple-tab-span"> </span>Like using randSeed to set the random generatorsfor each thread in sclang, <span class="Apple-converted-space"> </span></p>
358 <p class="p3"><span class="Apple-tab-span"> </span>you can choose which of several random generators on the server to use,</p>
359 <p class="p3"><span class="Apple-tab-span"> </span>and you can reset (seed) these random generators:<span class="Apple-converted-space"> </span></p>
360 <p class="p10"><span class="s10"><a href="../UGens/Random/RandID.html">RandID</a></span></p>
361 <p class="p10"><span class="s10"><a href="../UGens/Random/RandSeed.html">RandSeed</a></span></p>
362 <p class="p1"><br></p>
363 <p class="p1"><br></p>
364 <p class="p3"><b>UGens that generate random numbers on demand ("Demand UGens"):</b></p>
365 <p class="p1"><br></p>
366 <p class="p10"><span class="s10"><a href="../UGens/Synth control/Demand Rate/Dwhite.html">Dwhite</a></span></p>
367 <p class="p10"><span class="s10"><a href="../UGens/Synth control/Demand Rate/Dbrown.html">Dbrown</a></span></p>
368 <p class="p3">Diwhite</p>
369 <p class="p3">Dibrown</p>
370 <p class="p10"><span class="s10"><a href="../UGens/Synth control/Demand Rate/Drand.html">Drand</a></span></p>
371 <p class="p3">Dxrand</p>
372 <p class="p1"><br></p>
373 <p class="p3">see random patterns with analogous names</p>
374 <p class="p1"><br></p>
375 <p class="p1"><br></p>
376 <p class="p1"><br></p>
377 <p class="p4"><b>Random Patterns:<span class="Apple-converted-space"> </span></b></p>
378 <p class="p1"><br></p>
379 <p class="p1"><br></p>
380 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Prand.html">Prand</a></span><span class="s1">([ 1, 2, 3 ], </span><span class="s5">inf</span><span class="s1">);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// choose randomly one from a list ( list, numRepeats)</p>
381 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pxrand.html">Pxrand</a></span><span class="s1">([ 1, 2, 3 ]);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// choose one element from a list, no repeat of previous choice</p>
382 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pwhite.html">Pwhite</a></span><span class="s1">(24, 72);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// within range [&lt;hi&gt;, &lt;lo&gt;], choose a random value.</p>
383 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pbrown.html">Pbrown</a></span><span class="s1">(24, 72, 5)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// within range [&lt;hi&gt;, &lt;lo&gt;], do a random walk</p>
384 <p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// with a maximum &lt;step&gt; to the next value.</p>
385 <p class="p8"><span class="s5">Pgbrown</span><span class="s1">(24, 72, 1.4)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// geometric brownian motion</p>
386 <p class="p6"><br></p>
387 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Plprand.html">Plprand</a></span><span class="s13"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-converted-space"> </span></span></p>
388 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Phprand.html">Phprand<span class="s14"></span></a></span></p>
389 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Pmeanrand.html">Pmeanrand<span class="s14"></span></a></span></p>
390 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Pbeta.html">Pbeta<span class="s14"></span></a></span></p>
391 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Pcauchy.html">Pcauchy<span class="s14"></span></a></span></p>
392 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Pgauss.html">Pgauss<span class="s14"></span></a></span></p>
393 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Ppoisson.html">Ppoisson<span class="s14"></span></a></span></p>
394 <p class="p12"><span class="s10"><a href="../Streams-Patterns-Events/Patterns/Pexprand.html">Pexprand<span class="s14"></span></a></span></p>
395 <p class="p6"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
396 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pwrand.html">Pwrand</a></span><span class="s1">([ 1, 2, 3 ], [0.1, 0.3, 0.6], 20);<span class="Apple-tab-span"> </span></span>// choose from a list, probabilities by weights</p>
397 <p class="p6"><br></p>
398 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pshuf.html">Pshuf</a></span><span class="s1">([ 1, 2, 3, 4 ], 2);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// scramble the list, then repeat that order &lt;repeats&gt; times.</p>
399 <p class="p6"><br></p>
400 <p class="p7"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pwalk.html">Pwalk</a></span>( (0 .. 10), <span class="s5">Prand</span>([ -2,-1, 1, 2], <span class="s5">inf</span>));<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s9">// random walk.<span class="Apple-converted-space"> </span></span></p>
401 <p class="p6"><br></p>
402 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pfsm.html">Pfsm</a></span><span class="s1"> <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// random finite state machine pattern, see its help file.<span class="Apple-converted-space"> </span></p>
403 <p class="p8"><span class="s1"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// see also MarkovSet in MathLib quark</p>
404 <p class="p6"><br></p>
405 <p class="p8"><span class="s12"><a href="../Streams-Patterns-Events/Patterns/Pseed.html">Pseed</a></span><span class="s1">(seed, pattern)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// sets the random seed for that stream.</p>
406 <p class="p6"><br></p>
407 <p class="p6"><br></p>
408 <p class="p8">// some basic examples</p>
409 <p class="p6"><br></p>
410 <p class="p7">(</p>
411 <p class="p7"><span class="s5">Pbind</span>(<span class="s15">\note</span>, <span class="s5">Prand</span>([ 0, 2, 4 ], <span class="s5">inf</span>),<span class="Apple-converted-space"> </span></p>
412 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
413 <p class="p7">).play;</p>
414 <p class="p7">)</p>
415 <p class="p6"><br></p>
416 <p class="p7">(</p>
417 <p class="p13">Pbind<span class="s1">(</span></p>
418 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\note</span>, <span class="s5">Pxrand</span>([ 0, 2, 4 ], <span class="s5">inf</span>),</p>
419 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
420 <p class="p7">).play;</p>
421 <p class="p7">)</p>
422 <p class="p6"><br></p>
423 <p class="p7">(</p>
424 <p class="p13">Pbind<span class="s1">(</span></p>
425 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\note</span>, <span class="s5">Pwrand</span>([ 0, 2, 4 ], [0.1, 0.3, 0.6], <span class="s5">inf</span>),</p>
426 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
427 <p class="p7">).play;</p>
428 <p class="p7">)</p>
429 <p class="p6"><br></p>
430 <p class="p7">(</p>
431 <p class="p13">Pbind<span class="s1">(</span></p>
432 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\midinote</span>, <span class="s5">Pwhite</span>(48, 72, <span class="s5">inf</span>),</p>
433 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
434 <p class="p7">).play;</p>
435 <p class="p7">)</p>
436 <p class="p6"><br></p>
437 <p class="p7">(</p>
438 <p class="p13">Pbind<span class="s1">(</span></p>
439 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\midinote</span>, <span class="s5">Pbrown</span>(48, 72, 5, <span class="s5">inf</span>),</p>
440 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
441 <p class="p7">).play;</p>
442 <p class="p7">)</p>
443 <p class="p6"><br></p>
444 <p class="p7">(</p>
445 <p class="p13">Pbind<span class="s1">(</span></p>
446 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\midinote</span>, <span class="s5">Pgbrown</span>(48, 72, 0.5, <span class="s5">inf</span>).round,</p>
447 <p class="p7"><span class="Apple-tab-span"> </span><span class="s15">\dur</span>, 0.2</p>
448 <p class="p7">).play;</p>
449 <p class="p7">)</p>
450 <p class="p6"><br></p>
453 </body></html>